package com.anysoft.web;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebView;

import com.anysoft.WebViewNetworkProtocol;
import com.anysoft.event.TimeOutEvent;
import com.anysoft.me.base.BaseWebViewActivity;
import com.anysoft.utils.ActivityUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.MentorLogger;
import com.anysoft.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 定投
 * Created by wyb on 2017/3/26.
 */

public class FixedInvestmentActivity extends BaseWebViewActivity {

    private String token = "";
    private String url = "";

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShowTitleBar(true);
        title.setText("定投");
        if (getIntent().hasExtra("loadUrl")) {
            url = getIntent().getStringExtra("loadUrl");
        }
        if (StringUtils.isNullOrEmpty(url)) {
            finish();
        }
        loadUrl = url;
        if (CommonUtil.isNetWorkConnected(this)) {
            setWebViewConfig(loadUrl);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        reLoadPage(loadUrl);
    }

    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String url) {
        MentorLogger.Log(TAG, "url=" + url);
        if (url.contains("prompt.html")) {
            mIntent.setClass(mActivity, SelectWarmPromptActivity.class);
            mIntent.putExtra("url", url);
            startActivity(mIntent);
        } else if (url.contains("myfundInvest")) {
//            String param = url.split("\\?")[1];
//            System.out.println("param---" + param);
            mIntent.putExtra("url", url);
            mIntent.setClass(mActivity, MyFundInvestActivity.class);
            startActivity(mIntent);
        } else if (url.contains("InvestmentDetail")) {//定投详情
            mIntent.setClass(mActivity, InvestmentDetailActivity.class);
            mIntent.putExtra("url", url);
            startActivity(mIntent);
        } else if (url.contains(WebViewNetworkProtocol.GOTO_LOGIN)) {
            ActivityUtils.gotoLogin(mActivity, hasUser(), false, findUserId());
            EventBus.getDefault().post(new TimeOutEvent(true));
        } else if (url.contains(WebViewNetworkProtocol.GO_QUESTION)) {
            startRiskActivity(null);
        } else if (url.contains("fundInvestAdd.html")) {

            String url_decode = null;
            try {
                url_decode = URLDecoder.decode(url, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            MentorLogger.Log(TAG, "url_decode=" + url_decode);
            String mainTitle;
            Map<String, String> map = new HashMap<>();
            String[] params = url_decode.split("\\?")[1].split("&");
            for (int i = 0; i < params.length; i++) {
                System.out.println("params--" + params[i]);
                String key = params[i].split("=")[0];
                String value = "";
                try {
                    value = params[i].split("=")[1];
                } catch (Exception exception) {
                    MentorLogger.Log(TAG, "value为null");
                }
                map.put(key, value);
            }
            mainTitle = map.get("fundName") + " " + map.get("fundCode");
            mIntent.setClass(mActivity, AssetsFundInvestAddActivity.class);
            String param = url.split("\\?")[1];
            mIntent.putExtra("mainTitle", mainTitle);
            mIntent.putExtra("param", param);
            startActivity(mIntent);
        } else {
            webview.loadUrl(url);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
